<?php

namespace Admin\Controller;
use Think\Controller;
use Common\Api\Category;

class ArticleController extends CommonController{
	public function index() {
		$this->_list(D('ArticleView'));
		$this->display();
	}

	public function add() {
		if(IS_AJAX) {
			$db = D('Article');
			if( !$db->create() ) {
				$this->error( $db->getError() );
			}else{
				$db->uid = $_SESSION['uid'];
				if( !$db->add() ) {
					$this->error('新增失败！');
				}else{
					$this->success('新增成功！', U('index'));
				}
			}
		}else{
			$Category = new Category();
			$cate = M('Category')->select();
			$this->assign('category', $cate = $Category->unlimitedForLevel($cate) );
			$this->display();
		}
	}

	public function edit() {
		$db = D('Article');
		if(IS_AJAX) {
            if( !$db->create() ) {
				$this->error( $db->getError() );
			}else{
				if( !$db->where(array('id'=>I('post.id')))->save() ) {
					$this->error('更新失败！');
				}else{
					$this->success('更新成功！', U('index'));
				}
			}
		}else{
			$info = $db->where( array('id'=>I('get.id')) )->find();
			if(!$info) {
				$this->error('文章ID出错！');
			}
			$Category = new Category();
			$cate = M('Category')->select();
			$this->assign('category', $cate = $Category->unlimitedForLevel($cate) );
			$this->assign('info', $info);
			$this->display();
		}
	}

	/*public function del() {
		if(IS_AJAX) {
			if( !M('Article')->where(array('id'=>I('get.id')))->delete() ) {
				$this->error('删除失败！');
			}else{
				$this->success('删除成功！');
			}
		}else{
			E('非法访问！');
		}
	}*/

	public function uploadpic() {
		$upload = $this->_upload();
		echo json_encode($upload);
	}
}